<html>
<head><meta charset="utf-8"><title>What is needed for supporting attribute proc macro? · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html">What is needed for supporting attribute proc macro?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210373286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210373286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210373286">(Sep 17 2020 at 10:51)</a>:</h4>
<p>I'm seeking for some low hanging fruit to get my hands wet.</p>



<a name="210373370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210373370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210373370">(Sep 17 2020 at 10:52)</a>:</h4>
<p>Heh, I wouldn't call this low-hanging :)</p>



<a name="210373636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210373636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210373636">(Sep 17 2020 at 10:55)</a>:</h4>
<p>Attribute macros have to integrate with name resolution, since we can't add an item that still has unresolved attributes on it (they could be macros that change/delete the item).</p>
<p>I guess a relatively simple thing to do would be to extend name resolution to look at attributes, and to somehow add rustc's list of built-in attributes to rust-analyzer. I'm not totally sure what the best way to do that is, since they can change in arbitrary ways between nightly releases.</p>



<a name="210391677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210391677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210391677">(Sep 17 2020 at 13:46)</a>:</h4>
<p>Thanks! So i think i needs to deal with builtin attributes first. One thing that annoys me a little is that i'm almost using <code>thiserror</code> in every crate i writes, but <code>rust-analyzer</code> currently cannot auto-complete <code>thiserror::Error</code> for me. So i think i'll start from supporting <code>proc_macro_derive</code> attribute first.</p>



<a name="210392117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210392117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210392117">(Sep 17 2020 at 13:49)</a>:</h4>
<p>So this code in <code>thiserror</code>:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[proc_macro_derive(Error, attributes(backtrace, error, from, source))]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">derive_error</span><span class="p">(</span><span class="n">input</span>: <span class="nc">TokenStream</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">TokenStream</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parse_macro_input</span><span class="o">!</span><span class="p">(</span><span class="n">input</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">DeriveInput</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">expand</span>::<span class="n">derive</span><span class="p">(</span><span class="o">&amp;</span><span class="n">input</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">unwrap_or_else</span><span class="p">(</span><span class="o">|</span><span class="n">err</span><span class="o">|</span><span class="w"> </span><span class="n">err</span><span class="p">.</span><span class="n">to_compile_error</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">into</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>is a function and provides a <a href="https://docs.rs/thiserror/1.0.20/thiserror/#derives">"Derive Macro"</a> at the same time</p>



<a name="210392149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210392149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210392149">(Sep 17 2020 at 13:49)</a>:</h4>
<p>Ah, yeah, that's because name resolution of proc macros isn't done by rust-analyzer, but by querying Cargo and the proc-macro server, so it doesn't integrate well. I've been meaning to change that, but IIRC @\<strong>matklad</strong> had objections.</p>



<a name="210392387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210392387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210392387">(Sep 17 2020 at 13:51)</a>:</h4>
<p>ok...</p>



<a name="210392627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210392627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210392627">(Sep 17 2020 at 13:53)</a>:</h4>
<p>But i think <code>proc_macro_derive</code> itself is not a proc-macro but a builtin attribute? Do we want to see it as a proc_macro itself in ra?</p>



<a name="210393123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210393123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210393123">(Sep 17 2020 at 13:57)</a>:</h4>
<p>No, that should be built-in</p>



<a name="210393204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210393204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210393204">(Sep 17 2020 at 13:58)</a>:</h4>
<p>We have to add the name used in that attribute to the name resolution scope</p>



<a name="210393301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210393301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210393301">(Sep 17 2020 at 13:58)</a>:</h4>
<p>yeah</p>



<a name="210393746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210393746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210393746">(Sep 17 2020 at 14:01)</a>:</h4>
<p>I imagine this would be enough for <code>Error</code> to come out when auto-completion.</p>



<a name="210394603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210394603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210394603">(Sep 17 2020 at 14:07)</a>:</h4>
<p>So, if i do this change, should i create a <code>builtin_attribute.rs</code> module under <code>hir_expand</code>?</p>



<a name="210394908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210394908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210394908">(Sep 17 2020 at 14:09)</a>:</h4>
<p>That's one way to do it, yeah. rustc has a file like that, and it would be nice to have a way to keep them in sync more easily, but that can be done later.</p>



<a name="210397030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210397030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210397030">(Sep 17 2020 at 14:23)</a>:</h4>
<p>Thanks, i'll experiment to see what i can get :)</p>



<a name="210412257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210412257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210412257">(Sep 17 2020 at 16:09)</a>:</h4>
<p>In case you want to get closer to practice, one good implication of that would be the attribute completion: currently it's all hardcoded.</p>



<a name="210422745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210422745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210422745">(Sep 17 2020 at 17:30)</a>:</h4>
<p>Yeah, would be neat to find some way to load attribute and lint names from the sysroot at some point. But first this info has to actually be shipped through rustup.</p>



<a name="210422974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210422974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210422974">(Sep 17 2020 at 17:32)</a>:</h4>
<p>(I noticed that the new "unresolved import" diagnostics are quite intrusive when macros are being imported, so maybe I'll come up with a fix for that, or mark them as experimental)</p>



<a name="210425476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210425476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210425476">(Sep 17 2020 at 17:50)</a>:</h4>
<p>I'm tracing through the code, and found that... it seems to me that, to properly support attributes, i'll need to "detach" the current derive-supporting code, implement builtin attributes dispatching, and then "reattach" the <code>derive(..)</code> support to it as a special case...</p>



<a name="210425812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210425812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210425812">(Sep 17 2020 at 17:52)</a>:</h4>
<p>does <code>nameres</code> stand for <code>name resolution</code>? it seems it's actually doing most of the expansion work</p>



<a name="210428780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210428780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210428780">(Sep 17 2020 at 18:14)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116458">Charles Lew</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F/near/210425812">said</a>:</p>
<blockquote>
<p>does <code>nameres</code> stand for <code>name resolution</code>? it seems it's actually doing most of the expansion work</p>
</blockquote>
<p>yeah, it should call into other crates to expand any macros</p>



<a name="210539199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/What%20is%20needed%20for%20supporting%20attribute%20proc%20macro%3F/near/210539199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/What.20is.20needed.20for.20supporting.20attribute.20proc.20macro.3F.html#210539199">(Sep 18 2020 at 15:53)</a>:</h4>
<p>I've been working on the name resolution changes for proc macros I mentioned: <a href="https://github.com/rust-analyzer/rust-analyzer/pull/6033">https://github.com/rust-analyzer/rust-analyzer/pull/6033</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>